package srm520;

import java.util.Arrays;

public class SRMRoomAssignmentPhase {
    public int countCompetitors(int[] ratings, int K) {
        int myRating = ratings[0];
        Arrays.sort(ratings);
        int room = 0, a = 1, ind = 0;
        for (int i = ratings.length - 1; i >= 0; i--) {
            if (ratings[i] == myRating) {
                room = a % K;
                ind = i;
            }
            a++;
        }
//        if (room == 0) room = K;
        a = 1;
        int ans = 0;
        for (int i = ratings.length - 1; i >= 0; i--) {
            if (a % K == room && ind != i && ratings[i] > myRating)
                ans++;
            a++;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new SRMRoomAssignmentPhase().countCompetitors(new int[]{491, 981, 1199, 763, 994, 879, 888}, 3));
    }
}
